package frontend;

import javafx.embed.swing.JFXPanel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
/*import javafx.application.Platform;
import javafx.scene.media.Media;*/

public class FrontendApp {
    private JFrame frame;
    private JComboBox<String> videoList;
    private JButton playButton;
    private JPanel videoPanel;

    public FrontendApp() {
        frame = new JFrame("简单在线播放器");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 600);
        frame.setLayout(new BorderLayout());

        // 顶部控制面板
        JPanel controlPanel = new JPanel();
        videoList = new JComboBox<>();
        playButton = new JButton("播放");
        controlPanel.add(videoList);
        controlPanel.add(playButton);
        frame.add(controlPanel, BorderLayout.NORTH);

        // 视频显示面板
        videoPanel = new JPanel();
        frame.add(videoPanel, BorderLayout.CENTER);

        // 加载视频列表
        loadVideoList();

        // 播放按钮事件
        playButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String selectedVideo = (String) videoList.getSelectedItem();
                if (selectedVideo != null) {
                    playVideo(selectedVideo);
                }
            }
        });

        frame.setVisible(true);
    }

    private void loadVideoList() {
        try {
            URL url = new URL("http://localhost:8081/videos");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");

            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String response = reader.readLine();
            reader.close();

            String[] videos = response.split(",");
            for (String video : videos) {
                videoList.addItem(video);
            }
        } catch (IOException e) {
            JOptionPane.showMessageDialog(frame, "无法加载视频列表: " + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
            e.printStackTrace();
        }
    }

    private void playVideo(String videoId) {
        try {
            // 使用JavaFX播放视频 (IDEA内置)
            JFXPanel fxPanel = new JFXPanel();
            videoPanel.removeAll();
            videoPanel.add(fxPanel, BorderLayout.CENTER);
            videoPanel.revalidate();
            videoPanel.repaint();

            javafx.application.Platform.runLater(() -> {
                javafx.scene.media.Media media = new javafx.scene.media.Media(
                        "http://localhost:8081/video/" + videoId);
                javafx.scene.media.MediaPlayer mediaPlayer = new javafx.scene.media.MediaPlayer(media);
                javafx.scene.media.MediaView mediaView = new javafx.scene.media.MediaView(mediaPlayer);

                javafx.scene.Scene scene = new javafx.scene.Scene(
                        new javafx.scene.Group(mediaView), 720, 480);
                fxPanel.setScene(scene);

                mediaPlayer.play();
            });
        } catch (Exception e) {
            JOptionPane.showMessageDialog(frame, "无法播放视频: " + e.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new FrontendApp());
    }
}
